//
//  ChangePasswordView.m
//  TheRoadAD
//
//  Created by baixue on 16/8/15.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "ChangePasswordView.h"
#import "LoginViewController.h"

@interface ChangePasswordView ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *oldPassword;//旧密码
@property (weak, nonatomic) IBOutlet UITextField *password;//密码
@property (weak, nonatomic) IBOutlet UITextField *configPassword;//确认密码

@end

@implementation ChangePasswordView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
}

//点击修改密码
- (IBAction)changePassword:(id)sender {
    
    if ([_oldPassword.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入的旧密码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else  if ([_password.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入的新密码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else  if ([_configPassword.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请再次输入新密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else  if (![_password.text isEqualToString:_configPassword.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        
        [self getEditPassword];
    }
   
}


-(void)getEditPassword{
//URL:http://114.215.172.41:7756/UserInfo/EditPassword
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"Id"];
    NSString *key = [SHA1Secret getKeyWithActionName:@"EditPassword"];
    
    NSDictionary *para = @{
                           @"Id":userId,
                           @"OldPassword":_oldPassword.text,
                           @"Password":_password.text,
                           @"ConfigPassword":_configPassword.text,
                           @"Key":key
                               };
    [NetWorkManager requestWithType:HttpRequestTypePost withUrlString:@"UserInfo/EditPassword" withParaments:para
 withSuccessBlock:^(NSDictionary *object) {
     if ([object[@"code"] isEqualToString:@"100"]) {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
         [alert show];
     }
 } withFailureBlock:^(NSError *error) {
     NSLog(@"%@",error);
 } progress:nil];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    LoginViewController *l = [[LoginViewController alloc] init];
    [self presentViewController:l animated:YES completion:nil];
}

@end
